


//
//  PlayViewController.m
//  NeiHanDemo
//
//  Created by qianfeng on 16/2/20.
//  Copyright © 2016年 王卓越. All rights reserved.
//

#import "PlayViewController.h"
#import <MediaPlayer/MediaPlayer.h>


@interface PlayViewController ()

//MPMoviePlayerViewController
@property (nonatomic) MPMoviePlayerViewController *mp;

@end

@implementation PlayViewController

//视图即将出现的时候 调用的方法
- (void)viewWillAppear:(BOOL)animated{
    //重写父类的 viewWillAppear:
    [super viewWillAppear:animated];
    //隐藏上方的 NavigationBarHidden
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

//视图即将结束的时候 调用的方法
- (void)viewWillDisappear:(BOOL)animated{
    //重写父类的 viewWillDisappear :
    [super viewWillDisappear:animated];
    //显示上方的 NavigationBarHidden
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:self.videoUrl]];
    //自动播放
    self.mp.moviePlayer.shouldAutoplay = YES;
    self.mp.view.frame = self.view.bounds;
    //把播放器的view 添加到 MPMoviePlayerViewController 上
    [self.view addSubview:self.mp.view];
    
    //监控播放结束
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playEnd:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}

- (void)playEnd:(NSNotification *)notify{
//在该通知中可以判断出为何结束
    NSDictionary *dic = notify.userInfo;
    int status = [[dic objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    switch (status) {
        case 1:
            NSLog(@"正常播放结束");
            break;
        case 2:
            NSLog(@"用户结束");
            break;
        default:
            NSLog(@"异常结束");
            break;
    }
    //返回
    [self.navigationController popViewControllerAnimated:YES];
}

@end
